package tq.com.tqteacher.activity;


import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.browser.DetailPicsActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqteacher.adapt.TQJPLeaveMsgAdapter;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;

public class TQJPLeaveMsgActivity extends BaseTitleActivity {
    private TQJPLeaveMsgFragment mLeaveMsgFragment = new TQJPLeaveMsgFragment();

    static public void show(Activity pActivity){
        Intent lIntent = new Intent(pActivity, TQJPLeaveMsgActivity.class);
        pActivity.startActivity(lIntent);
    }
    @Override
    protected BaseFragment fragment() {
        return mLeaveMsgFragment;
    }

    static public class TQJPLeaveMsgFragment extends BaseRecyclerFragment{
        private TQJPLeaveMsgAdapter mLeaveMsgAdapter;
        private int page = 1;
        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);

            if(mLeaveMsgAdapter == null){
                mLeaveMsgAdapter = new TQJPLeaveMsgAdapter(getContext());
                mLeaveMsgAdapter.setIClickListener(mIClickListener);
            }
            pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            pRecyclerView.setAdapter(mLeaveMsgAdapter);
        }

        @Override
        protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
            pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    page++;
                    getData();
                }
            });
            pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    page = 1;
                    getData();
                }
            });
            pRefreshLayout.autoRefresh();
        }

        private void getData(){
            TQLoadingHelper.build(getContext()).setCancelable(false).show();
            TQNetInterface.getMeterialComment(TQPublishType.getSharePublishItem().getSeq_id(), page, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(mRefreshLayout.getState() == RefreshState.Loading){
                        mRefreshLayout.finishLoadMore();
                    }else{
                        mRefreshLayout.finishRefresh();
                    }
                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    ArrayList<TQAtachModel> lTQAtachModels = new ArrayList<>();
                    try {
                        if(pNetResult.getData() == null) return;

                        JSONArray all = pNetResult.getData().optJSONArray("list");
                        if(all == null) return;

                        for (int i = 0; i < all.length(); i++){
                            JSONObject lObject = all.optJSONObject(i);
                            if(lObject == null)continue;

                            JSONObject att = lObject.optJSONObject("attachment");
                            if(att == null)continue;

                            TQAtachModel lAtachModel = new TQAtachModel();
                            lAtachModel.fromJson(att);

                            JSONObject comm = lObject.optJSONObject("comment");
                            if(comm != null){
                                lAtachModel.refreshComment(comm);
                            }
                            lTQAtachModels.add(lAtachModel);
                        }

                        if(page == 1){
                            mLeaveMsgAdapter.clear();
                        }
                        mLeaveMsgAdapter.append(lTQAtachModels);
                    }finally {
                        TQLoadingHelper.dimiss();
                    }
                }
            });

        }

        private TQJPLeaveMsgAdapter.IClickListener mIClickListener = new TQJPLeaveMsgAdapter.IClickListener() {
            @Override
            public void openCover(TQAtachModel pModel) {
                ArrayList<String> lStrings = new ArrayList<>();
                String url = TQPictureUpload.downloadPath(pModel.getCover());
                lStrings.add(url);

                DetailPicsActivity.jumpToThis(TQJPLeaveMsgFragment.this.getActivity(), lStrings, 0);
            }

            @Override
            public void openComment(TQAtachModel pModel) {

            }
        };
    }
}
